<?php

class VirtualofficeController extends BaseController {

    public function postLogin() {
        if (Auth::user()->attempt(array(
                    'account_username' => Input::get('usernameagen'),
                    'password' => Input::get('passwordagen')
                ))) {
            $user = Auth::user()->get();
            Session::put('account_agen_id', $user->account_agen_id);
            Session::put('account_username', $user->account_username);
            return Redirect::to('/virtualoffice')->with('message', "Selamat datang " . $user->account_username);
        } else {
            return Redirect::back()->with('error', "<font color='red'> Username / password salah</font><br>");
        }
    }

    public function getLogout() {
        if(Session::get('agenlogin')<>0){
            Session::forget('agenlogin');
            Session::forget('account_agen_id');
            Session::forget('account_username');
            return Redirect::to('/agen')->with('message', "Anda telah berhasil logout dari account agen");
        }
        Auth::logout();
        Session::Flush();
        return Redirect::to('/')->with('message', "Anda telah berhasil logout");
    }

    public function getBeranda() {
       $jenis = DB::table('ref_brochure')
                    ->where('brochure_isdelete','0')
                    ->get();

        $brosurkertas = DB::table('ref_papertype')
                    ->where('papertype_type','=','brochure')
                    ->where('papertype_isdelete','0')
                    ->get();
        $envelopekertas = DB::table('ref_papertype')
                    ->where('papertype_type','=','envelope')
                    ->where('papertype_isdelete','0')
                    ->get();
        $flayerkertas = DB::table('ref_papertype')
                    ->where('papertype_type','=','flayer')
                    ->where('papertype_isdelete','0')
                    ->get();
        $letterheadkertas = DB::table('ref_papertype')
                    ->where('papertype_type','=','letterhead')
                    ->where('papertype_isdelete','0')
                    ->get();

        $brosursize = DB::table('ref_size_brochure')
                    ->where('size_brochure_isdelete','0')
                    ->get();
        $envelopesize = DB::table('ref_size_envelope')
                    ->where('size_envelope_isdelete','0')
                    ->get();
        $flayersize = DB::table('ref_size_flayer')
                    ->where('size_flayer_isdelete','0')
                    ->get();
        $letterheadsize = DB::table('ref_size_letterhead')
                    ->where('size_letterhead_isdelete','0')
                    ->get();

        $jenisidcard = DB::table('ref_idcard_type')
                    ->where('idcard_type_isdelete','0')
                    ->get();
        $kualitasidcard = DB::table('ref_idcard_quality')
                    ->where('idcard_quality_isdelete','0')
                    ->get();

        $jenisnota = DB::table('ref_bill')
                    ->where('bill_isdelete','0')
                    ->groupBy('bill_name')
                    ->get();

        $kertasnota = DB::table('ref_papertype')
                    ->where('papertype_isdelete','0')
                    ->where('papertype_type', '=', 'bill')
                    ->get();
        $jeniskaos = DB::table('ref_tshirt')
                ->groupBy('tshirt_type_of_material')
                ->where('tshirt_isdelete','0')
                ->get();


        $jenispackaging = DB::table('ref_packaging')
                    ->where('packaging_isdelete','0')
                    ->groupBy('packaging_type')
                    ->get();
        $ukuranpackaging = DB::table('ref_packaging')
                    ->where('packaging_isdelete','0')
                    ->groupBy('packaging_width')
                    ->get();
        $kertaspackaging = DB::table('ref_papertype')
                ->where('papertype_type', '=', 'packaging')
                ->where('papertype_isdelete','0')
                ->get();
        $jumlahpackaging = DB::table('ref_packaging')
                ->where('packaging_isdelete','0')
                ->groupBy('packaging_qty_price')
                ->get();

        $agen = DB::table('sys_agen')
                ->where('agen_id', Session::get('account_agen_id'))
                ->first();
        $agencode = $agen->agen_code;




        $opdby = DB::table('sys_order')
                ->where('order_name', 'packaging')
                ->where('order_agen_id', Session::get('account_agen_id'))
                ->whereBetween('order_date', array(date('Y-m-d', strtotime('-2 days')) . ' 00:00:00', date('Y-m-d', strtotime('-2 days')) . ' 23:59:59'))
                ->count();

        $opy = DB::table('sys_order')
                ->where('order_name', 'packaging')
                ->where('order_agen_id', Session::get('account_agen_id'))
                ->whereBetween('order_date', array(date('Y-m-d', strtotime('-1 days')) . ' 00:00:00', date('Y-m-d', strtotime('-1 days')) . ' 23:59:59'))
                ->count();



        if ($opy < $opdby) {
            $op = '<font color="red" size="4px">&#x25BC;  </font>' . $opy . ' Order';
        } else if ($opy > $opdby) {
            $op = '<font color="green" size="4px">&#x25B2;  </font>' . $opy . ' Order';
        } else {
            $op = '<font color="#FFD000" size="4px">=  </font>' . $opy . ' Order';
        }


        $oidby = DB::table('sys_order')
                ->where('order_name', 'idcard')
                ->where('order_agen_id', Session::get('account_agen_id'))
                ->whereBetween('order_date', array(date('Y-m-d', strtotime('-2 days')) . ' 00:00:00', date('Y-m-d', strtotime('-2 days')) . ' 23:59:59'))
                ->count();

        $oiy = DB::table('sys_order')
                ->where('order_name', 'idcard')
                ->where('order_agen_id', Session::get('account_agen_id'))
                ->whereBetween('order_date', array(date('Y-m-d', strtotime('-1 days')) . ' 00:00:00', date('Y-m-d', strtotime('-1 days')) . ' 23:59:59'))
                ->count();



        if ($oiy < $oidby) {
            $oi = '<font color="red" size="4px">&#x25BC;  </font>' . $oiy . ' Order';
        } else if ($oiy > $oidby) {
            $oi = '<font color="green" size="4px">&#x25B2;  </font>' . $oiy . ' Order';
        } else {
            $oi = '<font color="#FFD000" size="4px">=  </font>' . $oiy . ' Order';
        }


        $ondby = DB::table('sys_order')
                ->where('order_name', 'nota')
                ->where('order_agen_id', Session::get('account_agen_id'))
                ->whereBetween('order_date', array(date('Y-m-d', strtotime('-2 days')) . ' 00:00:00', date('Y-m-d', strtotime('-2 days')) . ' 23:59:59'))
                ->count();

        $ony = DB::table('sys_order')
                ->where('order_name', 'nota')
                ->where('order_agen_id', Session::get('account_agen_id'))
                ->whereBetween('order_date', array(date('Y-m-d', strtotime('-1 days')) . ' 00:00:00', date('Y-m-d', strtotime('-1 days')) . ' 23:59:59'))
                ->count();



        if ($ony < $ondby) {
            $on = '<font color="red" size="4px">&#x25BC;  </font>' . $ony . ' Order';
        } else if ($ony > $ondby) {
            $on = '<font color="green" size="4px">&#x25B2;  </font>' . $ony . ' Order';
        } else {
            $on = '<font color="#FFD000" size="4px">=  </font>' . $ony . ' Order';
        }


        $obdby = DB::table('sys_order')
                ->where('order_name', 'brochure')
                ->where('order_agen_id', Session::get('account_agen_id'))
                ->whereBetween('order_date', array(date('Y-m-d', strtotime('-2 days')) . ' 00:00:00', date('Y-m-d', strtotime('-2 days')) . ' 23:59:59'))
                ->count();

        $oby = DB::table('sys_order')
                ->where('order_name', 'brochure')
                ->where('order_agen_id', Session::get('account_agen_id'))
                ->whereBetween('order_date', array(date('Y-m-d', strtotime('-1 days')) . ' 00:00:00', date('Y-m-d', strtotime('-1 days')) . ' 23:59:59'))
                ->count();




        if ($oby < $obdby) {
            $ob = '<font color="red" size="4px">&#x25BC;  </font>' . $oby . ' Order';
        } else if ($oby > $obdby) {
            $ob = '<font color="green" size="4px">&#x25B2;  </font>' . $oby . ' Order';
        } else {
            $ob = '<font color="#FFD000" size="4px">=  </font>' . $oby . ' Order';
        }


        $otdby = DB::table('sys_order')
                ->where('order_name', 'tshirt')
                ->where('order_agen_id', Session::get('account_agen_id'))
                ->whereBetween('order_date', array(date('Y-m-d', strtotime('-2 days')) . ' 00:00:00', date('Y-m-d', strtotime('-2 days')) . ' 23:59:59'))
                ->count();

        $oty = DB::table('sys_order')
                ->where('order_name', 'tshirt')
                ->where('order_agen_id', Session::get('account_agen_id'))
                ->whereBetween('order_date', array(date('Y-m-d', strtotime('-1 days')) . ' 00:00:00', date('Y-m-d', strtotime('-1 days')) . ' 23:59:59'))
                ->count();



        if ($oty < $otdby) {
            $ot = '<font color="red" size="4px">&#x25BC;  </font>' . $oty . ' Order';
        } else if ($oty > $otdby) {
            $ot = '<font color="green" size="4px">&#x25B2;  </font>' . $oty . ' Order';
        } else {
            $ot = '<font color="#FFD000" size="4px">=  </font>' . $oty . ' Order';
        }


        $refdby = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', Session::get('account_agen_id'))
                ->whereBetween('bonus_log_date', array(date('Y-m-d', strtotime('-2 days')) . ' 00:00:00', date('Y-m-d', strtotime('-2 days')) . ' 23:59:59'))
                ->where('bonus_log_referal', '<>', 'Null')
                ->count();

        $refy = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', Session::get('account_agen_id'))
                ->whereBetween('bonus_log_date', array(date('Y-m-d', strtotime('-1 days')) . ' 00:00:00', date('Y-m-d', strtotime('-1 days')) . ' 23:59:59'))
                ->where('bonus_log_referal', '<>', 'Null')
                ->count();



        if ($refy < $refdby) {
            $ref = '<font color="red" size="4px"> &#x25BC;  </font>' . $refy . ' Agen';
        } else if ($refy > $refdby) {
            $ref = '<font color="green" size="4px"> &#x25B2;  </font>' . $refy . ' Agen';
        } else {
            $ref = '<font color="#FFD000" size="4px"> =  </font>' . $refy . ' Agen';
        }

        $frdby = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', Session::get('account_agen_id'))
                ->whereBetween('bonus_log_date', array(date('Y-m-d', strtotime('-2 days')) . ' 00:00:00', date('Y-m-d', strtotime('-2 days')) . ' 23:59:59'))
                ->sum('bonus_log_referal');

        $fry = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', Session::get('account_agen_id'))
                ->whereBetween('bonus_log_date', array(date('Y-m-d', strtotime('-1 days')) . ' 00:00:00', date('Y-m-d', strtotime('-1 days')) . ' 23:59:59'))
                ->sum('bonus_log_referal');
        if ($fry == "") {
            $fry = 0;
        }


        if ($fry < $frdby) {
            $fr = '<font color="red" size="4px">&#x25BC;  </font>Rp ' . number_format(intval($fry), 0, '.', '.') . '';
        } else if ($fry > $frdby) {
            $fr = '<font color="green" size="4px">&#x25B2;  </font>Rp ' . number_format(intval($fry), 0, '.', '.') . '';
        } else {
            $fr = '<font color="#FFD000" size="4px">=  </font>Rp ' . number_format(intval($fry), 0, '.', '.') . '';
        }

        $fodby = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', Session::get('account_agen_id'))
                ->whereBetween('bonus_log_date', array(date('Y-m-d', strtotime('-2 days')) . ' 00:00:00', date('Y-m-d', strtotime('-2 days')) . ' 23:59:59'))
                ->sum('bonus_log_order');

        $foy = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', Session::get('account_agen_id'))
                ->whereBetween('bonus_log_date', array(date('Y-m-d', strtotime('-1 days')) . ' 00:00:00', date('Y-m-d', strtotime('-1 days')) . ' 23:59:59'))
                ->sum('bonus_log_order');
        if ($foy == "") {
            $foy = 0;
        }


        if ($foy < $fodby) {
            $fo = '<font color="red" size="4px">&#x25BC;  </font>Rp ' . number_format(intval(round($foy)), 0, '.', '.') . '';
        } else if ($foy > $fodby) {
            $fo = '<font color="green" size="4px">&#x25B2;  </font>Rp ' . number_format(intval(round($foy)), 0, '.', '.') . '';
        } else {
            $fo = '<font color="#FFD000" size="4px">=  </font>Rp ' . number_format(intval(round($foy)), 0, '.', '.') . '';
        }

        return View::make('vo.beranda', compact('fo', $fo, 'fr', $fr, 'ref', $ref, 'op', $op, 'oi', $oi, 'on', $on, 'ob', $ob, 'ot', $ot, 'agencode', $agencode, 'kertaspackaging', $kertaspackaging, 'jumlahpackaging', $jumlahpackaging, 'ukuranpackaging', $ukuranpackaging, 'jenispackaging', $jenispackaging, 'jeniskaos', $jeniskaos, 'kertasnota', $kertasnota, 'jenisnota', $jenisnota, 'jenisidcard', $jenisidcard, 'kualitasidcard', $kualitasidcard, 'jenis', $jenis, 'brosurkertas', $brosurkertas, 'envelopekertas', $envelopekertas, 'letterheadkertas', $letterheadkertas, 'flayerkertas', $flayerkertas, 'brosursize', $brosursize, 'envelopesize', $envelopesize, 'letterheadsize', $letterheadsize, 'flayersize', $flayersize));
    }

    public function getDaftarharga() {
        if (Session::get('list') == "") {
            $list = 'idcard';
        } else {
            $list = Session::get('list');
        }


        if ($list == 'idcard') {
            $pricelist = refidcardquality::where('idcard_quality_isdelete', 0)->paginate(10);
        } else if ($list == 'packaging') {
            $pricelist = DB::table('ref_packaging')
                    ->join('ref_papertype', 'papertype_id', '=', 'packaging_papertype_id')
                    ->where('packaging_isdelete', 0)
                    ->paginate(10);
        } else if ($list == 'brochure') {
            $pricelist = refbrosur::where('brochure_isdelete', 0)->paginate(10);
        } else if ($list == 'nota') {
            $pricelist = refbill::where('bill_isdelete', 0)->paginate(10);
        } else if ($list == 'tshirt') {
            $pricelist = DB::table('ref_tshirt')
                    ->join('ref_tshirt_detail', 'tshirt_detail_tshirt_id', '=', 'tshirt_id')
                    ->where('tshirt_detail_isdelete', 0)
                    ->paginate(10);
        }
        return View::make('vo.daftarharga', compact('pricelist', $pricelist));
    }

    public function postDaftarharga() {
        if (Input::get('filter') == 'type') {
            Session::forget('filterby');
            Session::forget('keyword');
            Session::put('filter','type');
            Session::put('list', Input::get('typeby'));
            $list = Session::get('list');

            if ($list == 'idcard') {
                $pricelist = refidcardquality::where('idcard_quality_isdelete', 0)->paginate(10);
            } else if ($list == 'packaging') {
                $pricelist = DB::table('ref_packaging')
                        ->join('ref_papertype', 'papertype_id', '=', 'packaging_papertype_id')
                        ->where('packaging_isdelete', 0)
                        ->paginate(10);
            } else if ($list == 'brochure') {
                $pricelist = refbrosur::where('brochure_isdelete', 0)->paginate(10);
            } else if ($list == 'nota') {
                $pricelist = refbill::where('bill_isdelete', 0)->paginate(10);
            } else if ($list == 'tshirt') {
                $pricelist = DB::table('ref_tshirt')
                        ->join('ref_tshirt_detail', 'tshirt_detail_tshirt_id', '=', 'tshirt_id')
                        ->where('tshirt_detail_isdelete', 0)
                        ->paginate(10);
            }
        } if (Input::get('filter') == 'keyword') {
            Session::put('filter','keyword');
            Session::put('filterby',Input::get('filterby'));
            Session::put('keyword',Input::get('keyword'));
            $list = Session::get('list');
            if (Input::get('filterby') == 'name') {
                Session::put('list', Input::get('typeby'));
                if ($list == 'idcard') {
                    $pricelist = DB::table('ref_idcard_quality')
                            ->where('idcard_quality_name', 'LIKE', '%' . Input::get('keyword') . '%')
                            ->paginate(10);
                } else if ($list == 'nota') {
                    $pricelist = DB::table('ref_bill')
                            ->where('bill_name', 'LIKE', '%' . Input::get('keyword') . '%')
                            ->paginate(10);
                } else if ($list == 'brochure') {
                    $pricelist = DB::table('ref_brochure')
                            ->where('brochure_type', 'LIKE', '%' . Input::get('keyword') . '%')
                            ->paginate(10);
                } else if ($list == 'packaging') {
                    $pricelist = DB::table('ref_packaging')
                            ->join('ref_papertype', 'papertype_id', '=', 'packaging_papertype_id')
                            ->where('packaging_type', 'LIKE', '%' . Input::get('keyword') . '%')
                            ->paginate(10);
                } else if ($list == 'tshirt') {
                    $pricelist = DB::table('ref_tshirt')
                            ->join('ref_tshirt_detail', 'tshirt_detail_tshirt_id', '=', 'tshirt_id')
                            ->where('tshirt_type_of_material', 'LIKE', '%' . Input::get('keyword') . '%')
                            ->paginate(10);
                }
            }else if (Input::get('filterby') == 'harga') {
                if ($list == 'idcard') {
                    $pricelist = DB::table('ref_idcard_quality')
                            ->where('idcard_quality_price', 'LIKE', '%' . Input::get('keyword') . '%')
                            ->paginate(10);
                } else if ($list == 'nota') {
                    $pricelist = DB::table('ref_bill')
                            ->where('bill_price', 'LIKE', '%' . Input::get('keyword') . '%')
                            ->paginate(10);
                } else if ($list == 'brochure') {
                    $pricelist = DB::table('ref_brochure')
                            ->where('brochure_price', 'LIKE', '%' . Input::get('keyword') . '%')
                            ->paginate(10);
                } else if ($list == 'packaging') {
                    $pricelist = DB::table('ref_packaging')
                            ->join('ref_papertype', 'papertype_id', '=', 'packaging_papertype_id')
                            ->where('packaging_price', 'LIKE', '%' . Input::get('keyword') . '%')
                            ->paginate(10);
                } else if ($list == 'tshirt') {
                    $pricelist = DB::table('ref_tshirt')
                            ->join('ref_tshirt_detail', 'tshirt_detail_tshirt_id', '=', 'tshirt_id')
                            ->where('tshirt_detail_price', 'LIKE', '%' . Input::get('keyword') . '%')
                            ->paginate(10);
                }
            }
        }else{
            if(Session::get('filter')=="type"){
                    $list = Session::get('list');
                    if ($list == 'idcard') {
                        $pricelist = refidcardquality::where('idcard_quality_isdelete', 0)->paginate(10);
                    } else if ($list == 'packaging') {
                        $pricelist = DB::table('ref_packaging')
                                ->join('ref_papertype', 'papertype_id', '=', 'packaging_papertype_id')
                                ->where('packaging_isdelete', 0)
                                ->paginate(10);
                    } else if ($list == 'brochure') {
                        $pricelist = refbrosur::where('brochure_isdelete', 0)->paginate(10);
                    } else if ($list == 'nota') {
                        $pricelist = refbill::where('bill_isdelete', 0)->paginate(10);
                    } else if ($list == 'tshirt') {
                        $pricelist = DB::table('ref_tshirt')
                                ->join('ref_tshirt_detail', 'tshirt_detail_tshirt_id', '=', 'tshirt_id')
                                ->where('tshirt_detail_isdelete', 0)
                                ->paginate(10);
                    }
            }else if(Session::get('filter')=="keyword"){
                    $list = Session::get('list');
                    if (Session::get('filterby') == 'name') {
                        if ($list == 'idcard') {
                            $pricelist = DB::table('ref_idcard_quality')
                                    ->where('idcard_quality_name', 'LIKE', '%' . Session::get('keyword') . '%')
                                    ->paginate(10);
                        } else if ($list == 'nota') {
                            $pricelist = DB::table('ref_bill')
                                    ->where('bill_name', 'LIKE', '%' . Session::get('keyword') . '%')
                                    ->paginate(10);
                        } else if ($list == 'brochure') {
                            $pricelist = DB::table('ref_brochure')
                                    ->where('brochure_type', 'LIKE', '%' . Session::get('keyword') . '%')
                                    ->paginate(10);
                        } else if ($list == 'packaging') {
                            $pricelist = DB::table('ref_packaging')
                                    ->join('ref_papertype', 'papertype_id', '=', 'packaging_papertype_id')
                                    ->where('packaging_type', 'LIKE', '%' . Session::get('keyword') . '%')
                                    ->paginate(10);
                        } else if ($list == 'tshirt') {
                            $pricelist = DB::table('ref_tshirt')
                                    ->join('ref_tshirt_detail', 'tshirt_detail_tshirt_id', '=', 'tshirt_id')
                                    ->where('tshirt_type_of_material', 'LIKE', '%' . Session::get('keyword') . '%')
                                    ->paginate(10);
                        }
                    }else if (Session::get('filterby') == 'harga') {
                        if ($list == 'idcard') {
                            $pricelist = DB::table('ref_idcard_quality')
                                    ->where('idcard_quality_price', 'LIKE', '%' . Session::get('keyword') . '%')
                                    ->paginate(10);
                        } else if ($list == 'nota') {
                            $pricelist = DB::table('ref_bill')
                                    ->where('bill_price', 'LIKE', '%' . Session::get('keyword') . '%')
                                    ->paginate(10);
                        } else if ($list == 'brochure') {
                            $pricelist = DB::table('ref_brochure')
                                    ->where('brochure_price', 'LIKE', '%' . Session::get('keyword') . '%')
                                    ->paginate(10);
                        } else if ($list == 'packaging') {
                            $pricelist = DB::table('ref_packaging')
                                    ->join('ref_papertype', 'papertype_id', '=', 'packaging_papertype_id')
                                    ->where('packaging_price', 'LIKE', '%' . Session::get('keyword') . '%')
                                    ->paginate(10);
                        } else if ($list == 'tshirt') {
                            $pricelist = DB::table('ref_tshirt')
                                    ->join('ref_tshirt_detail', 'tshirt_detail_tshirt_id', '=', 'tshirt_id')
                                    ->where('tshirt_detail_price', 'LIKE', '%' . Session::get('keyword') . '%')
                                    ->paginate(10);
                        }
                    }
            }
        }

        return View::make('vo.daftarharga')->with('pricelist', $pricelist);
    }

    public function getProduk() {
      $jenis = DB::table('ref_brochure')
                    ->where('brochure_isdelete','0')
                    ->get();

        $brosurkertas = DB::table('ref_papertype')
                    ->where('papertype_type','=','brochure')
                    ->where('papertype_isdelete','0')
                    ->get();
        $envelopekertas = DB::table('ref_papertype')
                    ->where('papertype_type','=','envelope')
                    ->where('papertype_isdelete','0')
                    ->get();
        $flayerkertas = DB::table('ref_papertype')
                    ->where('papertype_type','=','flayer')
                    ->where('papertype_isdelete','0')
                    ->get();
        $letterheadkertas = DB::table('ref_papertype')
                    ->where('papertype_type','=','letterhead')
                    ->where('papertype_isdelete','0')
                    ->get();

        $brosursize = DB::table('ref_size_brochure')
                    ->where('size_brochure_isdelete','0')
                    ->get();
        $envelopesize = DB::table('ref_size_envelope')
                    ->where('size_envelope_isdelete','0')
                    ->get();
        $flayersize = DB::table('ref_size_flayer')
                    ->where('size_flayer_isdelete','0')
                    ->get();
        $letterheadsize = DB::table('ref_size_letterhead')
                    ->where('size_letterhead_isdelete','0')
                    ->get();

        $jenisidcard = DB::table('ref_idcard_type')
                    ->where('idcard_type_isdelete','0')
                    ->get();
        $kualitasidcard = DB::table('ref_idcard_quality')
                    ->where('idcard_quality_isdelete','0')
                    ->get();

        $jenisnota = DB::table('ref_bill')
                    ->where('bill_isdelete','0')
                    ->groupBy('bill_name')
                    ->get();

        $kertasnota = DB::table('ref_papertype')
                    ->where('papertype_isdelete','0')
                    ->where('papertype_type', '=', 'bill')
                    ->get();
        $jeniskaos = DB::table('ref_tshirt')
                ->groupBy('tshirt_type_of_material')
                ->where('tshirt_isdelete','0')
                ->get();


        $jenispackaging = DB::table('ref_packaging')
                    ->where('packaging_isdelete','0')
                    ->groupBy('packaging_type')
                    ->get();
        $ukuranpackaging = DB::table('ref_packaging')
                    ->where('packaging_isdelete','0')
                    ->groupBy('packaging_width')
                    ->get();
        $kertaspackaging = DB::table('ref_papertype')
                ->where('papertype_type', '=', 'packaging')
                ->where('papertype_isdelete','0')
                ->get();
        $jumlahpackaging = DB::table('ref_packaging')
                ->where('packaging_isdelete','0')
                ->groupBy('packaging_qty_price')
                ->get();

        $agen = DB::table('sys_agen')
                ->where('agen_id', Session::get('account_agen_id'))
                ->first();
        $agencode = $agen->agen_code;
        return View::make('vo.produk', compact('agencode', $agencode, 'kertaspackaging', $kertaspackaging, 'jumlahpackaging', $jumlahpackaging, 'ukuranpackaging', $ukuranpackaging, 'jenispackaging', $jenispackaging, 'jeniskaos', $jeniskaos, 'kertasnota', $kertasnota, 'jenisnota', $jenisnota, 'jenisidcard', $jenisidcard, 'kualitasidcard', $kualitasidcard, 'jenis', $jenis, 'brosurkertas', $brosurkertas, 'envelopekertas', $envelopekertas, 'letterheadkertas', $letterheadkertas, 'flayerkertas', $flayerkertas, 'brosursize', $brosursize, 'envelopesize', $envelopesize, 'letterheadsize', $letterheadsize, 'flayersize', $flayersize));
    }

    public function getDaftarpesanan() {
        Session::forget('list');
        Session::forget('filterby');
        Session::forget('keyword');
        Session::forget('dateby');
        $orderlistorder = DB::table('sys_order')
                ->join('sys_customer', 'order_customer_id', '=', 'customer_id')
                ->where('order_agen_id', Session::get('account_agen_id'))
                ->groupBy('order_id')
                ->orderBy('order_date', 'DESC')
                ->paginate(10);
        return View::make('vo.daftarpesanan')->with('orderlistorder', $orderlistorder);
    }

    public function postDaftarpesanan() {

        if (Input::get('filter') == 'date') {
            Session::forget('list');
            Session::forget('filterby');
            Session::forget('keyword');
            Session::put('filter','date');
                if(Input::get('datestart')==""){
                    return Redirect::back()->with('error', "<font color='red'> Harap masukan tanggal</font><br>");
                }
                if(Input::get('dateend')==""){
                    return Redirect::back()->with('error', "<font color='red'> Harap masukan tanggal</font><br>");
                }
            Session::put('datestart',Input::get('datestart'));
            Session::put('dateend',Input::get('dateend'));
            Session::put('dateby','all');

                    if(Input::get('dateby')=='all'){
                    $orderlistorder = DB::table('sys_order')
                                ->join('sys_customer','customer_id','=','order_customer_id')
                                ->where('order_agen_id', Session::get('account_agen_id'))
                                ->groupBy('order_id')
                                ->orderBy('order_date','DESC')
                                ->paginate(10);
                    }else if(Input::get('dateby')=='dateorder'){
                    $orderlistorder = DB::table('sys_order')
                                ->join('sys_customer','customer_id','=','order_customer_id')
                                ->where('order_agen_id', Session::get('account_agen_id'))
                                ->where('order_status','=','Delivered')
                                ->where('order_status_type','=','Pending')
                                ->whereBetween('order_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
                                ->groupBy('order_id')
                                ->orderBy('order_date','DESC')
                                ->paginate(5);
                    }else if(Input::get('dateby')=='dateapprove'){
                    $orderlistorder = DB::table('sys_order')
                                ->join('sys_customer','customer_id','=','order_customer_id')
                                ->where('order_agen_id', Session::get('account_agen_id'))
                                ->where('order_status','=','Delivered')
                                ->where('order_status_type','=','Approve')
                                ->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
                                ->groupBy('order_id')
                                ->orderBy('order_date','DESC')
                                ->paginate(5);
                    }else if(Input::get('dateby')=='dateprocess'){
                    $orderlistorder = DB::table('sys_order')
                                ->join('sys_customer','customer_id','=','order_customer_id')
                                ->where('order_agen_id', Session::get('account_agen_id'))
                                ->where('order_status','=','Delivered')
                                ->where('order_status_type','=','On Process')
                                ->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
                                ->groupBy('order_id')
                                ->orderBy('order_date','DESC')
                                ->paginate(5);
                    }else if(Input::get('dateby')=='datefinished'){
                    $orderlistorder = DB::table('sys_order')
                                ->join('sys_customer','customer_id','=','order_customer_id')
                                ->where('order_agen_id', Session::get('account_agen_id'))
                                ->where('order_status','=','Delivered')
                                ->where('order_status_type','=','Finished')
                                ->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
                                ->groupBy('order_id')
                                ->orderBy('order_date','DESC')
                                ->paginate(5);
                    }else if(Input::get('dateby')=='datedelivered'){
                    $orderlistorder = DB::table('sys_order')
                                ->join('sys_customer','customer_id','=','order_customer_id')
                                ->where('order_agen_id', Session::get('account_agen_id'))
                                ->where('order_status','=','Delivered')
                                ->where('order_status_type','=','Delivered')
                                ->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
                                ->groupBy('order_id')
                                ->orderBy('order_date','DESC')
                                ->paginate(5);
                    }
            return View::make('vo.daftarpesanan')->with('orderlistorder', $orderlistorder);
        } else if (Input::get('filter') == 'type') {
                Session::forget('filterby');
                Session::forget('keyword');
                Session::forget('dateby');
                Session::put('filter','type');
                Session::put('list', Input::get('typeby'));
                if(Input::get('typeby')=='all'){
                    $orderlistorder = DB::table('sys_order')
                        ->join('sys_customer', 'order_customer_id', '=', 'customer_id')
                        ->where('order_agen_id', Session::get('account_agen_id'))
                        ->groupBy('order_id')
                        ->orderBy('order_date', 'DESC')
                        ->paginate(10);
                }else{
                    $orderlistorder = DB::table('sys_order')
                        ->join('sys_customer', 'order_customer_id', '=', 'customer_id')
                        ->where('order_agen_id', Session::get('account_agen_id'))
                        ->where('order_name', '=', Input::get('typeby'))
                        ->groupBy('order_id')
                        ->orderBy('order_date', 'DESC')
                        ->paginate(10);
                }
                return View::make('vo.daftarpesanan')->with('orderlistorder', $orderlistorder);
        } else if (Input::get('filter') == 'keyword') {
            Session::put('filter','keyword');
            Session::forget('list');
            Session::forget('dateby');
            Session::put('filterby',Input::get('filterby'));
            Session::put('keyword',Input::get('keyword'));
            if (Input::get('filterby') == 'code') {
                $orderlistorder = DB::table('sys_order')
                        ->join('sys_customer', 'order_customer_id', '=', 'customer_id')
                        ->where('order_agen_id', Session::get('account_agen_id'))
                        ->where('order_code', 'LIKE', '%' . Input::get('keyword') . '%')
                        ->groupBy('order_id')
                        ->orderBy('order_date', 'DESC')
                        ->paginate(10);
                return View::make('vo.daftarpesanan')->with('orderlistorder', $orderlistorder);
            } else if (Input::get('filterby') == 'name') {
                $orderlistorder = DB::table('sys_order')
                        ->join('sys_customer', 'order_customer_id', '=', 'customer_id')
                        ->where('order_agen_id', Session::get('account_agen_id'))
                        ->where('customer_name', 'LIKE', '%' . Input::get('keyword') . '%')
                        ->groupBy('order_id')
                        ->orderBy('order_date', 'DESC')
                        ->paginate(10);
                return View::make('vo.daftarpesanan')->with('orderlistorder', $orderlistorder);
            }
        }else{
             if(Session::get('filter')=="date"){
                            if(Session::get('dateby')=='all'){
                            $orderlistorder = DB::table('sys_order')
                                        ->join('sys_customer','customer_id','=','order_customer_id')
                                        ->where('order_agen_id', Session::get('account_agen_id'))
                                        ->groupBy('order_id')
                                        ->orderBy('order_date','DESC')
                                        ->paginate(10);
                            }else if(Session::get('dateby')=='dateorder'){
                            $orderlistorder = DB::table('sys_order')
                                        ->join('sys_customer','customer_id','=','order_customer_id')
                                        ->where('order_agen_id', Session::get('account_agen_id'))
                                        ->where('order_status','=','Delivered')
                                        ->where('order_status_type','=','Pending')
                                        ->whereBetween('order_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
                                        ->groupBy('order_id')
                                        ->orderBy('order_date','DESC')
                                        ->paginate(5);
                            }else if(Session::get('dateby')=='dateapprove'){
                            $orderlistorder = DB::table('sys_order')
                                        ->join('sys_customer','customer_id','=','order_customer_id')
                                        ->where('order_agen_id', Session::get('account_agen_id'))
                                        ->where('order_status','=','Delivered')
                                        ->where('order_status_type','=','Approve')
                                        ->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
                                        ->groupBy('order_id')
                                        ->orderBy('order_date','DESC')
                                        ->paginate(5);
                            }else if(Session::get('dateby')=='dateprocess'){
                            $orderlistorder = DB::table('sys_order')
                                        ->join('sys_customer','customer_id','=','order_customer_id')
                                        ->where('order_agen_id', Session::get('account_agen_id'))
                                        ->where('order_status','=','Delivered')
                                        ->where('order_status_type','=','On Process')
                                        ->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
                                        ->groupBy('order_id')
                                        ->orderBy('order_date','DESC')
                                        ->paginate(5);
                            }else if(Session::get('dateby')=='datefinished'){
                            $orderlistorder = DB::table('sys_order')
                                        ->join('sys_customer','customer_id','=','order_customer_id')
                                        ->where('order_agen_id', Session::get('account_agen_id'))
                                        ->where('order_status','=','Delivered')
                                        ->where('order_status_type','=','Finished')
                                        ->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
                                        ->groupBy('order_id')
                                        ->orderBy('order_date','DESC')
                                        ->paginate(5);
                            }else if(Session::get('dateby')=='datedelivered'){
                            $orderlistorder = DB::table('sys_order')
                                        ->join('sys_customer','customer_id','=','order_customer_id')
                                        ->where('order_agen_id', Session::get('account_agen_id'))
                                        ->where('order_status','=','Delivered')
                                        ->where('order_status_type','=','Delivered')
                                        ->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
                                        ->groupBy('order_id')
                                        ->orderBy('order_date','DESC')
                                        ->paginate(5);
                            }
            }else if(Session::get('filter')=="type"){
                      if(Session::get('list')=='all'){
                            $orderlistorder = DB::table('sys_order')
                                ->join('sys_customer', 'order_customer_id', '=', 'customer_id')
                                ->where('order_agen_id', Session::get('account_agen_id'))
                                ->groupBy('order_id')
                                ->orderBy('order_date', 'DESC')
                                ->paginate(10);
                        }else{
                            $orderlistorder = DB::table('sys_order')
                                ->join('sys_customer', 'order_customer_id', '=', 'customer_id')
                                ->where('order_agen_id', Session::get('account_agen_id'))
                                ->where('order_name', '=', Session::get('list'))
                                ->groupBy('order_id')
                                ->orderBy('order_date', 'DESC')
                                ->paginate(10);
                        }
            }else if(Session::get('filter')=="keyword"){
                        if (Session::get('filterby') == 'code') {
                            $orderlistorder = DB::table('sys_order')
                                    ->join('sys_customer', 'order_customer_id', '=', 'customer_id')
                                    ->where('order_agen_id', Session::get('account_agen_id'))
                                    ->where('order_code', 'LIKE', '%' . Session::get('keyword') . '%')
                                    ->groupBy('order_id')
                                    ->orderBy('order_date', 'DESC')
                                    ->paginate(10);
                            return View::make('vo.daftarpesanan')->with('orderlistorder', $orderlistorder);
                        } else if (Session::get('filterby') == 'name') {
                            $orderlistorder = DB::table('sys_order')
                                    ->join('sys_customer', 'order_customer_id', '=', 'customer_id')
                                    ->where('order_agen_id', Session::get('account_agen_id'))
                                    ->where('customer_name', 'LIKE', '%' . Session::get('keyword') . '%')
                                    ->groupBy('order_id')
                                    ->orderBy('order_date', 'DESC')
                                    ->paginate(10);
                            return View::make('vo.daftarpesanan')->with('orderlistorder', $orderlistorder);
                        }
            }
        }

        
        return View::make('vo.daftarpesanan')->with('orderlistorder', $orderlistorder);
    }

    public function getKomisi() {
        $t = getdate();
        $t = $t[0];
        $t = date('Y', $t);

        $b = getdate();
        $b = $b[0];
        $b = date('m', $b);

        $d = getdate();
        $d = $d[0];
        $d = date('d', $d);

        $komisirefd = bonuslog::where('bonus_log_agen_id',Session::get('account_agen_id'))->whereBetween('bonus_log_date', array($t . '-' . $b . '-' . $d . ' 00:00:00', $t . '-' . $b . '-' . $d . ' 23:59:59'))->sum('bonus_log_referal');
        $komisiordd = bonuslog::where('bonus_log_agen_id',Session::get('account_agen_id'))->whereBetween('bonus_log_date', array($t . '-' . $b . '-' . $d . ' 00:00:00', $t . '-' . $b . '-' . $d . ' 23:59:59'))->sum('bonus_log_order');
        $komisireferal = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', Session::get('account_agen_id'))
                ->where('bonus_log_referal', '<>', 0)
                ->get();


        $komisiorder = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', Session::get('account_agen_id'))
                ->where('bonus_log_order', '<>', 0)
                ->get();
        $sumkomisiorder = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', Session::get('account_agen_id'))
                ->where('bonus_log_order', '<>', 0)
                ->sum('bonus_log_order');

        // echo'<pre>';
        // print_r($komisiall);	
        // echo'<pre>';

        $komisi = bonus::find(Session::get('account_agen_id'));
        $komisi->bonus_order = $sumkomisiorder;
        $komisi->Save();

        return View::make('vo.komisi', compact('komisi', $komisi, 'komisiorder', $komisiorder, 'komisireferal', $komisireferal, 'komisiordd', $komisiordd, 'komisirefd', $komisirefd));
    }

    public function getProfil() {
        $agen = DB::table('sys_agen')
                ->join('sys_account_agen', 'account_agen_id', '=', 'agen_id')
                ->join('sys_member', 'member_agen_id', '=', 'agen_id')
                ->where('agen_id', Session::get('account_agen_id'))
                ->first();
        if ($agen->member_picture == "") {
            $gambar = "images/agen/default.png";
        } else {
            $gambar = $agen->member_picture;
        }

        $provinsi = provinsi::all();
        $regional = regional::all();
        return View::make('vo.profil', compact('gambar', $gambar, 'provinsi', $provinsi, 'regional', $regional, 'agen', $agen));
    }

    //post pesan

    public function postIdcard() {
        if (Input::get('pilihan_jumlah') == "0") {
            $jumlah_idcard = 10;
        } else {
            $jumlah_idcard = Input::get('jumlah_idcard');
        }
        $pilihan_jumlah = Input::get('pilihan_jumlah');
        $jenis_idcard = Input::get('jenis_idcard');
        $kualitas_idcard = Input::get('kualitas_idcard');


        $customer = New customer();
        $customer->customer_name = Input::get('nama');
        $customer->customer_gender = Input::get('jenis_kelamin');
        $customer->customer_address = Input::get('alamat');
        $customer->customer_phone = Input::get('kontak');
        $customer->customer_email = Input::get('email');
        $customer->save();



        $order = New order();
        $order->order_customer_id = $customer->customer_id;
        $order->order_marketing_id = Input::get('marketing_id');
        $order->order_agen_id = Session::get('account_agen_id');
        $order->order_name = 'idcard';
        $order->order_qty = $jumlah_idcard;
        $order->order_qty_unit = 'Card';
        $order->order_total_price = Session::get('total_price_idcard');
        if (Input::hasFile('design_idcard')) {
            // ambil file
            $picture = Input::file('design_idcard');
            // simpan file ke directory upload yang terletak di folder public
            $picture->move(public_path() . '/design/idcard', $picture->getClientOriginalName());
            // simpan path ke file foto di database
            $order->order_desain = '/design/idcard/' . $picture->getClientOriginalName();
        }
        $order->order_date = date('Y-m-d H:i:s');
        $order->save();

        $order = order::Find($order->order_id);
        $order->order_code = $order->order_id . $customer->customer_id . date('md');
        $order->save();

        $typename = refidcardtypes::Find($jenis_idcard);
        $qualityname = refidcardquality::Find($kualitas_idcard);
        $orderidcard = New orderidcard();
        $orderidcard->order_idcard_order_id = $order->order_id;
        $orderidcard->order_idcard_idcard_quality_id = $kualitas_idcard;
        $orderidcard->order_idcard_quality_name = $qualityname->idcard_quality_name;
        $orderidcard->order_idcard_idcard_type_id = $jenis_idcard;
        $orderidcard->order_idcard_type_name = $typename->idcard_type_name;
        $orderidcard->save();

        $ref = DB::table('ref_additional_features')
                ->get();
        foreach ($ref as $ref) {
            if (Input::get($ref->additional_features_id) == '1') {

                $orderidcarddetail = New orderidcarddetail();
                $orderidcarddetail->order_idcard_detail_order_idcard_id = $orderidcard->order_idcard_id;
                $orderidcarddetail->order_idcard_detail_idcard_additional_features_id = $ref->additional_features_id;
                $orderidcarddetail->order_idcard_detail_feature_name = $ref->additional_features_name;
                $orderidcarddetail->save();
            }
        }

        $invoice = New invoice();
        $invoice->invoice_order_id = $order->order_id;
        $invoice->invoice_total_payment = Session::get('total_price_idcard');
        $invoice->invoice_status = 'Belum lunas';
        $invoice->save();

        $progress = New progress;
        $progress->order_progress_order_id = $order->order_id;
        $progress->save();

        $bonus = refbonus::where('bonus_config_name', 'order')->first();

        $bonuslog = New bonuslog();
        $bonuslog->bonus_log_agen_id = Session::get('account_agen_id');
        $bonuslog->bonus_log_order = round($bonus->bonus_config_percentage, 2) * $invoice->invoice_total_payment;
        $bonuslog->bonus_log_date = date('Y-m-d H:i:s');
        $bonuslog->save();

        $sumbonus = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', Session::get('account_agen_id'))
                ->sum('bonus_log_order');

        $rebonus = bonus::find(Session::get('account_agen_id'));
        $rebonus->bonus_order = $sumbonus;
        $rebonus->save();

        //send eail
        Mail::send('emails.terimakasih', array('pesan'=>'Terima kasih atas pesanan anda.','ordercode'=>$order->order_code), function($message){
        $email = Input::get('email');
        $nama = Input::get('nama');
        $message->to($email, $nama)->subject('Terima kasih');
        });
        
        Mail::send('emails.terimakasihorder', array('pesan'=>'Terima kasih atas pesanan anda.','ordercode'=>$order->order_code,'emailcust'=>Input::get('email'),'namecust'=>Input::get('nama')), function($message){
        $email = Session::get('account_username');
        $nama = Input::get('nama');
        $message->to($email, $nama)->subject('Terima kasih');
        });

        $result = DB::table('sys_order')
                ->join('sys_customer', 'customer_id', '=', 'order_customer_id')
                ->join('sys_order_idcard', 'order_idcard_order_id', '=', 'order_id')
                ->where('order_id', '=', $order->order_id)
                ->first();
        $ordername = 'Cetak ID Card';
        // Session::flush();
        Session::keep(array('username', 'email'));
        return View::make('vo.orderresult', compact('ordername', $ordername, 'result', $result));
    }

    public function postTshirt() {
        if (Input::get('pilihan_jumlah_kaos') == 1) {
            $qty = Input::get('jumlah_custom_kaos');
            $jumlah_custom = Input::get('jumlah_custom_kaos');
        } else if (Input::get('pilihan_jumlah_kaos') == 0) {
            $qty = Input::get('jumlah_standar_kaos');
            $jumlah_standar = Input::get('jumlah_standar_kaos');
        }
        $jenis_bahan_kaos = Input::get('jenis_bahan_kaos');

        $customer = New customer();
        $customer->customer_name = Input::get('nama');
        $customer->customer_gender = Input::get('jenis_kelamin');
        $customer->customer_address = Input::get('alamat');
        $customer->customer_phone = Input::get('kontak');
        $customer->customer_email = Input::get('email');
        $customer->save();

        $order = New order();
        $order->order_customer_id = $customer->customer_id;
        $order->order_name = 'tshirt';
        $order->order_marketing_id = Input::get('marketing_id');
        $order->order_agen_id = Session::get('account_agen_id');
        $order->order_qty = $qty;
        $order->order_qty_unit = 'Dozen';
        $order->order_total_price = Session::get('total_price_tshirt');
        if (Input::hasFile('design_kaos')) {
            // ambil file
            $picture = Input::file('design_kaos');
            // simpan file ke directory upload yang terletak di folder public
            $picture->move(public_path() . '/design/tshirt', $picture->getClientOriginalName());
            // simpan path ke file foto di database
            $order->order_desain = '/design/tshirt/' . $picture->getClientOriginalName();
        }
        $order->order_date = date('Y-m-d H:i:s');
        $order->save();

        $order = order::Find($order->order_id);
        $order->order_code = $order->order_id . $customer->customer_id . date('md');
        $order->save();

        $tshirt = New ordertshirt;
        $tshirt->order_tshirt_order_id = $order->order_id;
        $tshirt->order_tshirt_tshirt_id = Session::get('tshirt_id');
        $tshirt->save();

        $ref = DB::table('ref_additional_features')
                ->get();
        foreach ($ref as $ref) {
            if (Input::get($ref->additional_features_id) == '1') {

                $ordertshirtdetail = New ordertshirtdetail();
                $ordertshirtdetail->order_tshirt_detail_order_tshirt_id = $tshirt->order_tshirt_id;
                $ordertshirtdetail->order_tshirt_detail_additional_features_id = $ref->additional_features_id;
                $ordertshirtdetail->order_tshirt_detail_additional_features_name = $ref->additional_features_name;
                $ordertshirtdetail->save();
            }
        }

        $invoice = New invoice();
        $invoice->invoice_order_id = $order->order_id;
        $invoice->invoice_total_payment = Session::get('total_price_tshirt');
        $invoice->invoice_status = 'Belum lunas';
        $invoice->save();

        $progress = New progress;
        $progress->order_progress_order_id = $order->order_id;
        $progress->save();

        $bonus = refbonus::where('bonus_config_name', 'order')->first();

        $bonuslog = New bonuslog();
        $bonuslog->bonus_log_agen_id = Session::get('account_agen_id');
        $bonuslog->bonus_log_order = round($bonus->bonus_config_percentage, 2) * $invoice->invoice_total_payment;
        $bonuslog->bonus_log_date = date('Y-m-d H:i:s');
        $bonuslog->save();

        $sumbonus = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', Session::get('account_agen_id'))
                ->sum('bonus_log_order');

        $rebonus = bonus::find(Session::get('account_agen_id'));
        $rebonus->bonus_order = $sumbonus;
        $rebonus->save();

        //send eail
        Mail::send('emails.terimakasih', array('pesan'=>'Terima kasih atas pesanan anda.','ordercode'=>$order->order_code), function($message){
        $email = Input::get('email');
        $nama = Input::get('nama');
        $message->to($email, $nama)->subject('Terima kasih');
        });

        Mail::send('emails.terimakasihorder', array('pesan'=>'Terima kasih atas pesanan anda.','ordercode'=>$order->order_code,'emailcust'=>Input::get('email'),'namecust'=>Input::get('nama')), function($message){
        $email = Session::get('account_username');
        $nama = Input::get('nama');
        $message->to($email, $nama)->subject('Terima kasih');
        });
        

        $result = DB::table('sys_order')
                ->join('sys_customer', 'customer_id', '=', 'order_customer_id')
                ->join('sys_order_tshirt', 'order_tshirt_order_id', '=', 'order_id')
                ->where('order_id', '=', $order->order_id)
                ->first();
        $ordername = 'Cetak T-Shirt';
        // Session::flush();
        Session::keep(array('username', 'email'));
        return View::make('vo.orderresult', compact('ordername', $ordername, 'result', $result));
    }

    public function postNota() {
        $jenis_nota = Input::get('jenis_nota');
        $jenis_kertas = Input::get('jenis_kertas');
        $jumlah_warna = Input::get('jumlah_warna');
        $qty = Input::get('jumlah_order');


        $customer = New customer();
        $customer->customer_name = Input::get('nama');
        $customer->customer_gender = Input::get('jenis_kelamin');
        $customer->customer_address = Input::get('alamat');
        $customer->customer_phone = Input::get('kontak');
        $customer->customer_email = Input::get('email');
        $customer->save();

        $order = New order();
        $order->order_customer_id = $customer->customer_id;
        $order->order_name = 'nota';
        $order->order_marketing_id = Input::get('marketing_id');
        $order->order_agen_id = Session::get('account_agen_id');
        $order->order_qty = $qty;
        $order->order_qty_unit = 'Rim';
        $order->order_total_price = Session::get('total_price_nota');
        if (Input::hasFile('design_nota')) {
            // ambil file
            $picture = Input::file('design_nota');
            // simpan file ke directory upload yang terletak di folder public
            $picture->move(public_path() . '/design/nota', $picture->getClientOriginalName());
            // simpan path ke file foto di database
            $order->order_desain = '/design/nota/' . $picture->getClientOriginalName();
        }
        $order->order_date = date('Y-m-d H:i:s');
        $order->save();

        $order = order::Find($order->order_id);
        $order->order_code = $order->order_id . $customer->customer_id . date('md');
        $order->save();


        $billid = refbill::find(Session::get('bill_id'));


        $bill = New ordernota;
        $bill->order_bill_order_id = $order->order_id;

        $bill->order_bill_bill_id = Session::get('bill_id');
        $bill->order_bill_qty_ply = $billid->bill_qty_ply;
        $bill->order_bill_size = $billid->bill_size;
        $bill->order_bill_name = $billid->bill_name;
        $bill->order_bill_qty_color = $jumlah_warna;
        $paper = refpapertypes::find($jenis_kertas);
        $bill->order_bill_papertype = $paper->papertype_name;
        $bill->save();



        $invoice = New invoice();
        $invoice->invoice_order_id = $order->order_id;
        $invoice->invoice_total_payment = Session::get('total_price_nota');
        $invoice->invoice_status = 'Belum lunas';
        $invoice->save();

        $progress = New progress;
        $progress->order_progress_order_id = $order->order_id;
        $progress->save();

        $bonus = refbonus::where('bonus_config_name', 'order')->first();

        $bonuslog = New bonuslog();
        $bonuslog->bonus_log_agen_id = Session::get('account_agen_id');
        $bonuslog->bonus_log_order = round($bonus->bonus_config_percentage, 2) * $invoice->invoice_total_payment;
        $bonuslog->bonus_log_date = date('Y-m-d H:i:s');
        $bonuslog->save();

        $sumbonus = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', Session::get('account_agen_id'))
                ->sum('bonus_log_order');

        $rebonus = bonus::find(Session::get('account_agen_id'));
        $rebonus->bonus_order = $sumbonus;
        $rebonus->save();

        //send eail
        Mail::send('emails.terimakasih', array('pesan'=>'Terima kasih atas pesanan anda.','ordercode'=>$order->order_code), function($message){
        $email = Input::get('email');
        $nama = Input::get('nama');
        $message->to($email, $nama)->subject('Terima kasih');
        });

       Mail::send('emails.terimakasihorder', array('pesan'=>'Terima kasih atas pesanan anda.','ordercode'=>$order->order_code,'emailcust'=>Input::get('email'),'namecust'=>Input::get('nama')), function($message){
        $email = Session::get('account_username');
        $nama = Input::get('nama');
        $message->to($email, $nama)->subject('Terima kasih');
        });
        

        $result = DB::table('sys_order')
                ->join('sys_customer', 'customer_id', '=', 'order_customer_id')
                ->join('sys_order_bill', 'order_bill_order_id', '=', 'order_id')
                ->where('order_id', '=', $order->order_id)
                ->first();
        $ordername = 'Cetak Nota';
        // Session::flush();
        Session::keep(array('username', 'email'));
        return View::make('vo.orderresult', compact('ordername', $ordername, 'result', $result));
    }

    public function postPackaging() {
        if (Input::get('pilihan_ukuran') == 1) {
            Session::put('length', Input::get('length'));
            Session::put('width', Input::get('width'));
            Session::put('height', Input::get('height'));
            Session::put('jenis_ukuran', Input::get('length') . ' x ' . Input::get('width') . ' x ' . Input::get('height'));
        } else {
            Session::put('ukuran_standar', Input::get('ukuran_standar_packaging'));
            $size = DB::table('ref_packaging')
                    ->where('packaging_id', Input::get('ukuran_standar_packaging'))
                    ->first();
            Session::put('length', $size->packaging_length);
            Session::put('width', $size->packaging_width);
            Session::put('height', $size->packaging_height);
            Session::put('jenis_ukuran', $size->packaging_length . ' x ' . $size->packaging_width . ' x ' . $size->packaging_height);
        }

        if (Input::get('pilihan_jumlah') == 1) {
            Session::put('qty', Input::get('jumlah_custom_packaging'));
            Session::put('jumlah_custom', Input::get('jumlah_custom_packaging'));
        } else {
            Session::put('qty', Input::get('jumlah_standar_packaging'));
            Session::put('jumlah_standar', Input::get('jumlah_standar_packaging'));
        }

        $jenis_packaging = Input::get('jenis_packaging');
        $jenis_kertas = Input::get('jenis_kertas_packaging');
        $pilihan_jumlah = Input::get('pilihan_jumlah_packaging');
        $pilihan_ukuran = Input::get('pilihan_ukuran');




        $customer = New customer();
        $customer->customer_name = Input::get('nama');
        $customer->customer_gender = Input::get('jenis_kelamin');
        $customer->customer_address = Input::get('alamat');
        $customer->customer_phone = Input::get('kontak');
        $customer->customer_email = Input::get('email');
        $customer->save();

        $order = New order();
        $order->order_customer_id = $customer->customer_id;
        $order->order_name = 'packaging';
        $order->order_marketing_id = Input::get('marketing_id');
        $order->order_agen_id = Session::get('account_agen_id');
        $order->order_qty = Session::get('qty');
        $order->order_qty_unit = 'Pcs';
        $order->order_total_price = Session::get('total_price_packaging');
        if (Input::hasFile('design_packaging')) {
            // ambil file
            $picture = Input::file('design_packaging');
            // simpan file ke directory upload yang terletak di folder public
            $picture->move(public_path() . '/design/packaging', $picture->getClientOriginalName());
            // simpan path ke file foto di database
            $order->order_desain = '/design/packaging/' . $picture->getClientOriginalName();
        }
        $order->order_date = date('Y-m-d H:i:s');
        $order->save();

        $order = order::Find($order->order_id);
        $order->order_code = $order->order_id . $customer->customer_id . date('md');
        $order->save();

        $packaging = New orderpackaging;
        $packaging->order_packaging_order_id = $order->order_id;
        $packaging->order_packaging_packaging_id = Session::get('packaging_id');
        $packaging->order_packaging_type = $jenis_packaging;
        $packaging->order_packaging_size = Session::get('jenis_ukuran');
        $packaging->order_packaging_paper = $jenis_kertas;
        $packaging->save();

        $ref = DB::table('ref_additional_features')
                ->get();
        foreach ($ref as $ref) {
            if (Input::get($ref->additional_features_id) == '1') {

                $orderpackagingdetail = New orderpackagingdetail();
                $orderpackagingdetail->order_packaging_detail_order_packaging_id = $packaging->order_packaging_id;
                $orderpackagingdetail->order_packaging_detail_additional_features_id = $ref->additional_features_id;
                $orderpackagingdetail->order_packaging_detail_additional_features_name = $ref->additional_features_name;
                $orderpackagingdetail->save();
            }
        }

        $invoice = New invoice();
        $invoice->invoice_order_id = $order->order_id;
        $invoice->invoice_total_payment = Session::get('total_price_packaging');
        $invoice->invoice_status = 'Belum lunas';
        $invoice->save();

        $progress = New progress;
        $progress->order_progress_order_id = $order->order_id;
        $progress->save();

        $bonus = refbonus::where('bonus_config_name', 'order')->first();

        $bonuslog = New bonuslog();
        $bonuslog->bonus_log_agen_id = Session::get('account_agen_id');
        $bonuslog->bonus_log_order = round($bonus->bonus_config_percentage, 2) * $invoice->invoice_total_payment;
        $bonuslog->bonus_log_date = date('Y-m-d H:i:s');
        $bonuslog->save();

        $sumbonus = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', Session::get('account_agen_id'))
                ->sum('bonus_log_order');

        $rebonus = bonus::find(Session::get('account_agen_id'));
        $rebonus->bonus_order = $sumbonus;
        $rebonus->save();

        //send eail
        Mail::send('emails.terimakasih', array('pesan'=>'Terima kasih atas pesanan anda.','ordercode'=>$order->order_code), function($message){
        $email = Input::get('email');
        $nama = Input::get('nama');
        $message->to($email, $nama)->subject('Terima kasih');
        });
        
        Mail::send('emails.terimakasihorder', array('pesan'=>'Terima kasih atas pesanan anda.','ordercode'=>$order->order_code,'emailcust'=>Input::get('email'),'namecust'=>Input::get('nama')), function($message){
        $email = Session::get('account_username');
        $nama = Input::get('nama');
        $message->to($email, $nama)->subject('Terima kasih');
        });

        $result = DB::table('sys_order')
                ->join('sys_customer', 'customer_id', '=', 'order_customer_id')
                ->join('sys_order_packaging', 'order_packaging_order_id', '=', 'order_id')
                ->where('order_id', '=', $order->order_id)
                ->first();
        $ordername = 'Cetak Packaging';
        // Session::flush();
        Session::keep(array('username', 'email'));
        return View::make('vo.orderresult', compact('ordername', $ordername, 'result', $result));
    }

    public function postBrosur() {


        $jenis_cetak = Input::get('jenis_cetak');
        $jenis_kertas = Input::get('jenis_kertas');
        $brosur_size = Input::get('brosur_size');
        $warna = Input::get('warna');
        $qty_print_page = Input::get('qty_print_page');
        $qty = Input::get('order_brosur_qty');
        $brosur_unit = Input::get('order_brosur_qty_unit');



        $customer = New customer();
        $customer->customer_name = Input::get('nama');
        $customer->customer_gender = Input::get('jenis_kelamin');
        $customer->customer_address = Input::get('alamat');
        $customer->customer_phone = Input::get('kontak');
        $customer->customer_email = Input::get('email');
        $customer->save();

        $order = New order();
        $order->order_customer_id = $customer->customer_id;
        $order->order_name = 'brochure';
        $order->order_marketing_id = Input::get('marketing_id');
        $order->order_agen_id = Session::get('account_agen_id');
        $order->order_qty = $qty;
        $order->order_qty_unit = $brosur_unit;
        $order->order_total_price = Session::get('total_price_brosur');
        if (Input::hasFile('design_brosur')) {
            // ambil file
            $picture = Input::file('design_brosur');
            // simpan file ke directory upload yang terletak di folder public
            $picture->move(public_path() . '/design/brosur', $picture->getClientOriginalName());
            // simpan path ke file foto di database
            $order->order_desain = '/design/brosur/' . $picture->getClientOriginalName();
        }
        $order->order_date = date('Y-m-d H:i:s');
        $order->save();

        $order = order::Find($order->order_id);
        $order->order_code = $order->order_id . $customer->customer_id . date('md');
        $order->save();



      if(Session::get('brosur_id')<>0){
                            $brosurid = refbrosur::find(Session::get('brosur_id'));

                            if($brosurid->brochure_type == "brochure"){
                                $size = refbrochuresize::find(Input::get('brosur_size'));
                                $sizename = $size->size_brochure_name;
                            }else if($brosurid->brochure_type == "envelope"){
                                $size = refenvelopesize::find(Input::get('brosur_size'));
                                $sizename = $size->size_envelope_length.' x '.$size->size_envelope_width.' '. $size->size_envelope_unit;
                            }else if($brosurid->brochure_type == "flayer"){
                                $size = refflayersize::find(Input::get('brosur_size'));
                                $sizename = $size->size_flayer_length.' x '.$size->size_flayer_width.' '. $size->size_flayer_unit;
                            }else if($brosurid->brochure_type == "letterhead"){
                                $size = refletterheadsize::find(Input::get('brosur_size'));
                                $sizename = $size->size_letterhead_length.' '. $size->size_letterhead_width.' '. $size->size_letterhead_unit;
                            }
        }elseif(Session::get('brosur_id')==0){
                            if(Input::get('jenis_cetak')=="brochure"){
                                $size = refbrochuresize::find(Input::get('brosur_size'));
                                $sizename = $size->size_brochure_name;
                            }else if(Input::get('jenis_cetak')=="envelope"){
                                $size = refenvelopesize::find(Input::get('brosur_size'));
                                $sizename = $size->size_envelope_length.' x '.$size->size_envelope_width.' '. $size->size_envelope_unit;
                            }else if(Input::get('jenis_cetak')=="flayer"){
                                $size = refflayersize::find(Input::get('brosur_size'));
                                $sizename = $size->size_flayer_length.' x '.$size->size_flayer_width.' '. $size->size_flayer_unit;
                            }else if(Input::get('jenis_cetak')=="letterhead"){
                                $size = refletterheadsize::find(Input::get('brosur_size'));
                                $sizename = $size->size_letterhead_length.' '. $size->size_letterhead_width.' '. $size->size_letterhead_unit;
                            }
        }

        $brochure = New orderbrochure;
        $brochure->order_brochure_order_id = $order->order_id;

        $brochure->order_brochure_brochure_id = Session::get('brosur_id');
        $brochure->order_brochure_size = $sizename;

        $brochure->order_brochure_type = $jenis_cetak ;
        $brochure->order_brochure_color = $warna;
        $brochure->order_brochure_qty_print_page = $qty_print_page;
        $kertas = refpapertypes::find($jenis_kertas);
        $brochure->order_brochure_papertype = $kertas->papertype_name;
        $brochure->save();



        $invoice = New invoice();
        $invoice->invoice_order_id = $order->order_id;
        $invoice->invoice_total_payment = Session::get('total_price_brosur');
        $invoice->invoice_status = 'Belum lunas';
        $invoice->save();

        $progress = New progress;
        $progress->order_progress_order_id = $order->order_id;
        $progress->save();

        $bonus = refbonus::where('bonus_config_name', 'order')->first();

        $bonuslog = New bonuslog();
        $bonuslog->bonus_log_agen_id = Session::get('account_agen_id');
        $bonuslog->bonus_log_order = round($bonus->bonus_config_percentage, 2) * $invoice->invoice_total_payment;
        $bonuslog->bonus_log_date = date('Y-m-d H:i:s');
        $bonuslog->save();

        $sumbonus = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', Session::get('account_agen_id'))
                ->sum('bonus_log_order');

        $rebonus = bonus::find(Session::get('account_agen_id'));
        $rebonus->bonus_order = $sumbonus;
        $rebonus->save();

        //send eail
        Mail::send('emails.terimakasih', array('pesan'=>'Terima kasih atas pesanan anda.','ordercode'=>$order->order_code), function($message){
        $email = Input::get('email');
        $nama = Input::get('nama');
        $message->to($email, $nama)->subject('Terima kasih');
        });



       Mail::send('emails.terimakasihorder', array('pesan'=>'Terima kasih atas pesanan anda.','ordercode'=>$order->order_code,'emailcust'=>Input::get('email'),'namecust'=>Input::get('nama')), function($message){
        $email = Session::get('account_username');
        $nama = Input::get('nama');
        $message->to($email, $nama)->subject('Terima kasih');
        });



        $result = DB::table('sys_order')
                ->join('sys_customer', 'customer_id', '=', 'order_customer_id')
                ->join('sys_order_brochure', 'order_brochure_order_id', '=', 'order_id')
                ->where('order_id', '=', $order->order_id)
                ->first();
        $ordername = 'Cetak Brosur';
        // Session::flush();
        Session::keep(array('username', 'email'));
        return View::make('vo.orderresult', compact('ordername', $ordername, 'result', $result));
    }

    public function postEditprofile($id) {
        $member = member::Find($id);
        $member->member_name = Input::get('member_name');
        $member->member_address = Input::get('member_address');
        $member->member_phone = Input::get('member_phone');
        $member->member_email = Input::get('member_email');
        $member->member_regional_id = Input::get('member_regional_id');
        $member->member_provinsi_id = Input::get('member_provinsi_id');
        $member->save();

        return Redirect::to('/virtualoffice/profil')->with('message', 'Profile berhasil diubah');
    }

    public function postEditpassword($id) {
        $rules = array(
            'password' => 'required|confirmed',
            'password_confirmation' => 'required',
        );
        $notif = array(
            'confirmed' => ' <font color="red"> Harap masukan pasword yang sama.</font><br>',);

        $validator = Validator::make(Input::all(), $rules, $notif);

        if ($validator->fails()) {
            return Redirect::back()->with('error', 'Harap masukan pasword yang sama.');
        }
        $agen = accountagen::find($id);
        $agen->account_password = Hash::make(Input::get('password'));
        $agen->save();
        return Redirect::to('/virtualoffice/profil')->with('message', 'Password berhasil diganti');
    }

    public function postEditgambar($id) {
        $agen = member::find($id);

        File::delete($agen->member_picture);
        $images = Input::file('images');
        $images->move(public_path() . '/images/agen', $images->getClientOriginalName());
        $agen->member_picture = 'images/agen/' . $images->getClientOriginalName();

        $agen->save();
        return Redirect::to('/virtualoffice/profil')->with('message', 'Gambar berhasil diganti');
    }


    public function getAgen()
    {
        Session::forget('list');
        Session::forget('filter');
        Session::forget('filterby');
        Session::forget('keyword');
        Session::forget('datestart');
        Session::forget('dateend');
        $dataagen = DB::table('sys_agen')
                    ->join('sys_member','member_agen_id','=','agen_id')
                    ->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
                    ->join('ref_regional','member_regional_id','=','regional_id')
                    ->where('agen_sponsor_id',Session::get('account_agen_id'))
                    ->paginate(10);
        $provinsi = provinsi::all();
        $regional = regional::all();
        return View::make('vo.agen',compact('provinsi',$provinsi,'regional',$regional,'dataagen',$dataagen));
    }


    public function postAgen()
    {
        if(Input::get('filter')=='type'){
            Session::forget('filterby');
            Session::forget('keyword');
            Session::put('filter','type');
            Session::put('list', Input::get('typeby'));
            if(Input::get('typeby')=='all'){
                $dataagen = DB::table('sys_agen')
                    ->join('sys_member','member_agen_id','=','agen_id')
                    ->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
                    ->join('ref_regional','member_regional_id','=','regional_id')
                    ->where('agen_sponsor_id',Session::get('account_agen_id'))
                    ->paginate(10);
            }else {
                $dataagen = DB::table('sys_agen')
                    ->join('sys_member','member_agen_id','=','agen_id')
                    ->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
                    ->join('ref_regional','member_regional_id','=','regional_id')
                    ->where('agen_sponsor_id',Session::get('account_agen_id'))
                    ->where('member_status','=',Input::get('typeby'))
                    ->paginate(10);
            }
        }else if(Input::get('filter')=='keyword'){
            Session::put('filter','keyword');
            Session::forget('list');
            Session::put('filterby',Input::get('filterby'));
            Session::put('keyword',Input::get('keyword'));
            if(Input::get('filterby')=='agencode'){
            $dataagen = DB::table('sys_agen')
                    ->join('sys_member','member_agen_id','=','agen_id')
                    ->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
                    ->join('ref_regional','member_regional_id','=','regional_id')
                    ->where('agen_sponsor_id',Session::get('account_agen_id'))
                    ->where('agen_code','LIKE','%'.Input::get('keyword').'%')
                    ->paginate(10); 
            }else if(Input::get('filterby')=='agenname'){
            $dataagen = DB::table('sys_agen')
                    ->join('sys_member','member_agen_id','=','agen_id')
                    ->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
                    ->join('ref_regional','member_regional_id','=','regional_id')
                    ->where('agen_sponsor_id',Session::get('account_agen_id'))
                    ->where('member_name','LIKE','%'.Input::get('keyword').'%')
                    ->paginate(10);
            }         
        }else{
           if(Session::get('filter')=="type"){
                    if(Session::get('list')=='all'){
                        $dataagen = DB::table('sys_agen')
                            ->join('sys_member','member_agen_id','=','agen_id')
                            ->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
                            ->join('ref_regional','member_regional_id','=','regional_id')
                            ->where('agen_sponsor_id',Session::get('account_agen_id'))
                            ->paginate(10);
                    }else {
                        $dataagen = DB::table('sys_agen')
                            ->join('sys_member','member_agen_id','=','agen_id')
                            ->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
                            ->join('ref_regional','member_regional_id','=','regional_id')
                            ->where('agen_sponsor_id',Session::get('account_agen_id'))
                            ->where('member_status','=',Session::get('list'))
                            ->paginate(10);
                    }
            }else if(Session::get('filter')=="keyword"){
                    if(Session::get('filterby')=='agencode'){
                    $dataagen = DB::table('sys_agen')
                            ->join('sys_member','member_agen_id','=','agen_id')
                            ->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
                            ->join('ref_regional','member_regional_id','=','regional_id')
                            ->where('agen_sponsor_id',Session::get('account_agen_id'))
                            ->where('agen_code','LIKE','%'.Session::get('keyword').'%')
                            ->paginate(10); 
                    }else if(Session::get('filterby')=='agenname'){
                    $dataagen = DB::table('sys_agen')
                            ->join('sys_member','member_agen_id','=','agen_id')
                            ->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
                            ->join('ref_regional','member_regional_id','=','regional_id')
                            ->where('agen_sponsor_id',Session::get('account_agen_id'))
                            ->where('member_name','LIKE','%'.Session::get('keyword').'%')
                            ->paginate(10);
                    }
            }
        }   


        $provinsi = provinsi::all();
        $regional = regional::all();
        return View::make('vo.agen',compact('provinsi',$provinsi,'regional',$regional,'dataagen',$dataagen));
    }


    public function postTambahagen()
    {

        $agen = New agen();
        $agen->agen_admin_group_id = 2;
        $agen->agen_sponsor_id = Session::get('account_agen_id');
        $agen->save();

        $agencode = agen::find($agen->agen_id);
            $hasil = $agen->agen_id + 1;
            $pk_n = strlen($hasil); 
            if($pk_n == 1){$noagen = "000".$hasil;}
            if($pk_n == 2){$noagen = "00".$hasil;}
            if($pk_n == 3){$noagen = "0".$hasil;}
            if($pk_n == 4){$noagen = $hasil;}
        $agencode->agen_code = 'AGEN'.$noagen;
        $agencode->save();

        $detailagen = New member();
        $detailagen->member_agen_id = $agen->agen_id;
            $reg_id = regional::where('regional_name',Input::get('member_regional_name'))->first();
        $detailagen->member_regional_id = $reg_id->regional_id;
        $detailagen->member_provinsi_id = Input::get('member_provinsi_id');
        $detailagen->member_name = Input::get('member_name');
        $detailagen->member_phone = Input::get('member_phone');
        $detailagen->member_email = Input::get('member_email');
        $detailagen->member_address = Input::get('member_address');
        $detailagen->member_joindate = date('Y-m-d H:i:s');
        $detailagen->member_status = 'Tidak aktif';
        $detailagen->save();


        $bonus = refbonus::where('bonus_config_name','referal')->first();

        $bonuslog = New bonuslog();
        $bonuslog->bonus_log_agen_id = Session::get('account_agen_id');
        $bonuslog->bonus_log_referal = $bonus->bonus_config_percentage;
        $bonuslog->bonus_log_date = date('Y-m-d H:i:s');
        $bonuslog->save();

        $bonus = New bonus();
        $bonus->bonus_agen_id = $agen->agen_id;
        $bonus->bonus_referal = 0;
        $bonus->bonus_order = 0;
        $bonus->save();

        $sumbonus = DB::table('sys_bonus_log')
                    ->where('bonus_log_agen_id',Session::get('account_agen_id'))
                    ->sum('bonus_log_referal');

        $rebonus = bonus::find(Session::get('account_agen_id'));
        $rebonus->bonus_referal = $sumbonus;
        $rebonus->save();

        return Redirect::to('/virtualoffice/agen')->with('message','Pendaftaran agen baru sukses');
    }


}